<template>
  <div class="profile-page">

  <div class="user-info">
    <div class="container">
      <div class="row">

        <div class="col-xs-12 col-md-10 offset-md-1">
          <img :src="profile.image" class="user-img" />
          <h4>{{ username }}</h4>
          <p class="ng-binding">{{ profile.bio }}</p>
          <button
            v-if="!isAuthor"
            class="btn btn-sm btn-outline-secondary action-btn"
            @click="onFollow(profile)"
            :disabled="profile.followDisabled">
            <i class="ion-plus-round"></i>
            &nbsp;
            <span v-if="profile.following">Unfollow</span><span v-if="!profile.following">Follow</span> {{ username }}
          </button>
          <nuxt-link
            v-if="isAuthor"
            class="btn btn-sm btn-outline-secondary action-btn"
            to="/settings">
            <i class="ion-gear-a"></i> Edit Profile Settings
          </nuxt-link>
        </div>

      </div>
    </div>
  </div>

  <div class="container">
    <div class="row">

      <div class="col-xs-12 col-md-10 offset-md-1">
        <div class="articles-toggle">
          <ul class="nav nav-pills outline-active">
            <li class="nav-item">
              <nuxt-link
                class="nav-link"
                :class="{
                  active: tab === 'my_article'
                }"
                exact
                :to="{
                  name: 'profile',
                  params: {
                    username: username
                  },
                  query: {
                    tab: 'my_article'
                  }
                }">My Articles</nuxt-link>
            </li>
            <li class="nav-item">
              <nuxt-link
                class="nav-link"
                :class="{
                  active: tab === 'favorited_article'
                }"
                exact
                :to="{
                  name: 'profile',
                  params: {
                    username: username
                  },
                  query: {
                    tab: 'favorited_article'
                  }
                }">Favorited Articles</nuxt-link>
            </li>
          </ul>
        </div>

        <div
          class="article-preview"
          v-for="article in articles"
          :key="article.slug">
          <div class="article-meta">
            <nuxt-link :to="{
              name: 'profile',
              params: {
                username: article.author.username
              }
            }">
              <img
                :src="article.author.image"
              />
            </nuxt-link>
            <div class="info">
              <nuxt-link
                class="author"
                :to="{
                  name: 'profile',
                  params: {
                    username: article.author.username
                  }
                }">
                {{ article.author.username }}
              </nuxt-link>
              <span class="date">{{ article.createdAt | date('MMM DD YYYY') }}</span>
            </div>
            <button class="btn btn-outline-primary btn-sm pull-xs-right"
              :class="{
                active: article.favorited
              }"
              @click="onFavorite(article)"
              :disabled="article.favoriteDisabled"
            >
              <i class="ion-heart"></i> {{ article.favoritesCount }}
            </button>
          </div>
          <nuxt-link
            class="preview-link"
            :to="{
              name: 'article',
              params: {
                slug: article.slug
              }
            }"
          >
            <h1>{{ article.title }}</h1>
            <p>{{ article.description }}</p>
            <span>Read more...</span>
            <ul class="tag-list">
              <li class="tag-default tag-pill tag-outline" v-for="(tag, index) in article.tagList" :key="index">{{ tag }}</li>
            </ul>
          </nuxt-link>
        </div>


      </div>

    </div>
  </div>

</div>
</template>

<script>
import { mapState } from 'vuex'

import { getArticles, deleteFavorite, addFavorite } from '@/api/article'
import { getProfile, follow, unfollow } from '@/api/user'

export default {
  name: 'UserProfile',
  middleware: 'authenticated',
  watchQuery: ['tab'],
  async asyncData({ query, params }) {
    const { username } = params
    const tab = query.tab || 'my_article'
    const [articleRes, profileRes] = await Promise.all([
      getArticles({
        limit: 5,
        offset: 0,
        ...(tab === 'my_article' ? {
          author: username
        } : {
          favorited: username
        })
      }),
      getProfile(username)
    ])
    const { articles } = articleRes.data
    const { profile } = profileRes.data

    return {
      articles,
      username,
      profile,
      tab
    }
  },
  computed: {
    ...mapState(['user']),
    isAuthor() {
      if (!this.user || !this.username) {
        return false
      }
      return this.user.username === this.username
    }
  },
  methods: {
    async onFavorite(article) {
      if (!this.user) {
        return
      }
      article.favoriteDisabled = true
      if (article.favorited) {
        await deleteFavorite(article.slug)
        article.favorited = false
        article.favoritesCount += -1
      } else {
        await addFavorite(article.slug)
        article.favorited = true
        article.favoritesCount += 1
      }
      article.favoriteDisabled = false
    },
    async onFollow(profile) {
      if (!this.user) {
        return
      }
      profile.followDisabled = true
      if (profile.following) {
        await unfollow(profile.username)
        profile.following = false
      } else {
        await follow(profile.username)
        profile.following = true
      }
      profile.followDisabled = false
    }
  }
}
</script>

<style>

</style>